package com.roger.bt.trans.ser.service;

import android.annotation.TargetApi;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.Service;
import android.bluetooth.BluetoothDevice;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.os.IBinder;
import android.util.Log;

import com.roger.bt.trans.ser.BtBase;
import com.roger.bt.trans.ser.BtServer;
import com.roger.bt.trans.ser.Listener;
import com.roger.bt.trans.ser.MainActivity;
import com.roger.bt.trans.ser.R;
import com.roger.bt.trans.ser.util.Logger;

public class ReceiveService extends Service implements Listener {
    private static String TAG = "Test" + ReceiveService.class.getSimpleName();
    private static final String ID = "com.roger.bt.trans.ser.service";
    private static final String NAME = "receive service";
    private boolean again = false;

    private BtServer server = new BtServer(this);

    public ReceiveService() { }

    @Override
    public void onCreate() {
        super.onCreate ();
        setForeground();
        MainActivity.wakeLock(this);
    }

    @Override
    public void onDestroy(){
        super.onDestroy ();
        server.unListener();
        server.close();
    }

    @Override
    public int onStartCommand(Intent intent,int flags,int startId){
        return super.onStartCommand (intent,flags,startId);
    }

    @Override
    public IBinder onBind(Intent intent) {
        // TODO: Return the communication channel to the service.
        throw new UnsupportedOperationException("Not yet implemented");
    }

    @TargetApi(26)
    private void setForeground() {
        NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        NotificationChannel channel = new NotificationChannel(ID, NAME, NotificationManager.IMPORTANCE_HIGH);
        manager.createNotificationChannel(channel);
        Notification notification = new Notification.Builder(this, ID)
                .setContentText("receive service is running")
                .setSmallIcon(R.mipmap.ic_launcher)
                .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher))
                .build();
        startForeground(1, notification);
    }

    @Override
    public void socketNotify(int state, Object obj) {
        switch (state) {
            case Listener.CONNECTED:
                BluetoothDevice dev = (BluetoothDevice) obj;
                Log.d(TAG,String.format("Connect to %s(%s) success", dev.getName(), dev.getAddress()));
                again = true;
                break;
            case Listener.DISCONNECTED:
                if(again){
                    Log.d(TAG,"device disconnect");
                    Logger.log(BtBase.FILE_PATH +"logs.txt","==device disconnect==");
                    again = false;
                }
                server.listen();
                break;
            case Listener.MSG:
                Log.d(TAG,obj.toString());
                break;
        }
    }
}
